package com.future.mayi.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.view.CircleImageView;
import com.future.mayi.R;
import com.future.mayi.activity.InviteNextActivity;
import com.future.mayi.model.InviteItemModel;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;


public class InviteAdapter extends BaseRecyclerViewAdapter<InviteItemModel> {

    private boolean enableIntent = true;

    public InviteAdapter(Context context, XRecyclerView recyclerView) {
        super(context, null, recyclerView);
        if (context instanceof InviteNextActivity)
            enableIntent = false;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(getView(parent, R.layout.layout_invite_item));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        ViewHolder holder = (ViewHolder) viewHolder;
        if (enableIntent)
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    InviteNextActivity.startActivity(mContext, getItem(position));
                }
            });
        InviteItemModel model = getItem(position);
        int drawable = 0;
        switch (model.getLevel()) {
            case 1:
                drawable = R.drawable.img_dl_m_1;
                break;
            case 10:
                drawable = R.drawable.img_dl_m_2;
                break;
            case 20:
                drawable = R.drawable.img_dl_m_3;
                break;
            case 30:
                drawable = R.drawable.img_dl_m_4;
                break;
        }
        if (drawable != 0) {
            holder.ivLevel.setImageResource(drawable);
            holder.ivLevel.setVisibility(View.VISIBLE);
        } else
            holder.ivLevel.setVisibility(View.GONE);
        loadImg(holder.ivHeadImg, model.getAvatar());
        setText(holder.tvName, model.getNickname());
        setText(holder.tvPhone, model.getMobile());
        setText(holder.tvTime, model.getCreatetime());
        holder.ivIntent.setVisibility(enableIntent ? View.VISIBLE : View.GONE);
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_head_img)
        CircleImageView ivHeadImg;
        @BindView(R.id.iv_level)
        ImageView ivLevel;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_phone)
        TextView tvPhone;
        @BindView(R.id.tv_states)
        TextView tvStates;

        @BindView(R.id.iv_intent)
        View ivIntent;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
